package com.tqyan.service.chat;

import com.tqyan.model.dto.ChatContentDTO;
import com.tqyan.model.dto.ChatDTO;
import com.tqyan.model.dto.UserDTO;
import com.tqyan.model.dto.UserSettingDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 聊天指令上下文
 * <p> <功能详细描述> </p>
 *
 * @author 陈晨
 * @version 1.0
 * @date 2023/5/4
 */
@Component
public class ChatPointContext {
    private static final Map<String, ChatPointService> CHAT_POINT_MAP = new HashMap<>();

    @Autowired
    private List<ChatPointService> chatPointList;

    @PostConstruct
    public void init() {
        for (ChatPointService chatPoint : chatPointList) {
            if (null == chatPoint) {
                continue;
            }
            CHAT_POINT_MAP.put(chatPoint.getPoint(), chatPoint);
        }
    }

    public ChatPointService get(String point) {
        return CHAT_POINT_MAP.get(point);
    }

    /**
     * @description 聊天
     * <p> <功能详细描述> </p>
     *
     * @author 陈晨
     * @date 2023/5/4 15:38
     */
    public ChatContentDTO chat(UserDTO user, UserSettingDTO userSetting, ChatDTO chat, String content) {
        // 匹配指令
        ChatPointService chatPoint = this.get(content
                .replaceAll("：", ":")
                .split(":")[0]);
        if (null != chatPoint) {
            ChatContentDTO chatContent = chatPoint.chat(user, userSetting, chat, content);
            if (null != chatContent) {
                return chatContent;
            }
        }
        // 没有匹配指令: 默认聊天
        return this.get(null).chat(user, userSetting, chat, content);
    }

}


